package com.xh.bussiness.tiku.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.bussiness.tiku.bean.dto.QuestionCartDto;
import com.xh.bussiness.tiku.bean.dto.QuestionCartItemDto;
import com.xh.bussiness.tiku.dao.QuestionCartItemMapper;
import com.xh.bussiness.tiku.dao.QuestionCartMapper;
import com.xh.bussiness.tiku.model.QuestionCart;
import com.xh.bussiness.tiku.model.QuestionCartItem;
import com.xh.bussiness.tiku.service.IQuestionCartService;
import com.xh.bussiness.tiku.service.IQuestionService;
import com.xh.core.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/*
 * @Author daiwei
 * @Description
 * @Date 2019/3/18 13:55
 **/
public class QuestionCartServiceImpl extends BaseServiceImpl<QuestionCartMapper, QuestionCart> implements IQuestionCartService {

    @Autowired
    private QuestionCartItemMapper questionCartItemMapper;
    @Autowired
    private IQuestionService questionService;

    @Override
    public void add(String userId, String cartType, String questionId) {
        //判断是否已包含此题
        QueryWrapper qw=new QueryWrapper();
        qw.eq("type",cartType);
        qw.eq("creator_id",userId);
        QuestionCart qc=this.dao.selectOne(qw);

        if(qc==null){
            qc=new QuestionCart();
            qc.setType(cartType);
            qc.setQuestionCount(1);
            this.insert(qc);
        }

        QueryWrapper qwi=new QueryWrapper();
        qwi.eq("cart_id",qc.getId());
        qwi.eq("question_id",questionId);
        QuestionCartItem questionCartItem=this.questionCartItemMapper.selectOne(qwi);
        if(questionCartItem==null){
            questionCartItem=new QuestionCartItem();
            questionCartItem.setCartId(qc.getId());
            questionCartItem.setQuestionId(questionId);
            this.questionCartItemMapper.insert(questionCartItem);
        }
    }

    @Override
    public void delete(String userId, String cartType, String questionId) {
        //判断是否已包含此题
        QueryWrapper qw=new QueryWrapper();
        qw.eq("type",cartType);
        qw.eq("creator_id",userId);
        QuestionCart qc=this.dao.selectOne(qw);

        if(qc==null){
            return;
        }

        QueryWrapper qwi=new QueryWrapper();
        qwi.eq("cart_id",qc.getId());
        qwi.eq("question_id",questionId);
        this.questionCartItemMapper.delete(qwi);
    }

    @Override
    public void deleteAll(String userId, String cartType) {
        //判断是否已包含此题
        QueryWrapper qw=new QueryWrapper();
        qw.eq("type",cartType);
        qw.eq("creator_id",userId);
        QuestionCart qc=this.dao.selectOne(qw);

        if(qc==null){
            return;
        }

        QueryWrapper qwi=new QueryWrapper();
        qwi.eq("cart_id",qc.getId());
        this.questionCartItemMapper.delete(qwi);
   }

    @Override
    public QuestionCartDto getByCartType(String userId, String cartType) {
        QuestionCartDto result=new QuestionCartDto();
        result.setQuestionCount(0);
        result.setType(cartType);
        //判断是否已包含此题
        QueryWrapper qw=new QueryWrapper();
        qw.eq("type",cartType);
        qw.eq("creator_id",userId);
        QuestionCart qc=this.dao.selectOne(qw);

        if(qc==null){
            return result;
        }

        QueryWrapper qwi=new QueryWrapper();
        qwi.eq("cart_id",qc.getId());
        List<QuestionCartItem> items= this.questionCartItemMapper.selectList(qwi);
        if(items!=null){
            result.setQuestionCount(items.size());
            List<QuestionCartItemDto> itemDtos=new ArrayList<>();
            for(QuestionCartItem item:items){
                QuestionCartItemDto dto=new QuestionCartItemDto();
                dto.setId(item.getId());
                dto.setQuestionId(item.getQuestionId());
                dto.setQuestionCartId(qc.getId());
                dto.setQuestion(questionService.getQuestion(dto.getQuestionId(),null));
                itemDtos.add(dto);
            }
            result.setQuestions(itemDtos);
        }
        return result;
    }
}
